<template>

  <div class="download-iframe-page">
      <iframe :id="iframeName" :style="iframeStyle"></iframe>
  </div>

</template>

<script>
  import {createFileUrl, createDownloadUrl} from '@/utils/downloadUtil';
  import { getToken} from "@/utils/auth"
  import Vue from 'vue'

  export default {
    name:'DownloadIframe',
    props:{
      height:{
        type: Number,
        default: 40
      },
      width:{
        type: Number,
        default: 560
      }
    },
    data(){
      return {
        iframeName: '',
        sign:'',
        isListening: false
      }
    },
    created(){
      this.iframeName = 'subDownloadPage_'+new Date().getTime(),
      this.addErrorTipListener();
    },
    beforeUnmount(){
      this.deleteErrorTipListener()
    },
    computed:{
      iframeStyle(){
        return {
          height: this.height + 'px',
          width: this.width + 'px',
          border: 'none'
        }
      }
    },
    methods:{
      download(data, url){
        this.sign = data.s;
        let downloadUrl;
        let params;
        if(url){
          downloadUrl = createDownloadUrl(url, data);
          params = Object.assign(data, { _a:getToken() });
        }else{
          downloadUrl = createFileUrl(data);
          params = { t: data.t, s: data.s, _a:getToken() };
        }
        let iframeWindow = document.getElementById(this.iframeName).contentWindow;
        let form = iframeWindow.document.createElement("form");
        form.setAttribute("id", "_downloadform_");
        form.setAttribute("style", "display:none");
        form.setAttribute("target", "");
        form.setAttribute("method", "get");
        form.setAttribute("action", downloadUrl);
        for (let key in params) {
          let input1 = iframeWindow.document.createElement("input");
          input1.setAttribute("type", "hidden");
          input1.setAttribute("name", key);
          input1.setAttribute("value", params[key]);
          form.appendChild(input1);
        }
        iframeWindow.document.body.appendChild(form);
        form.submit();
      },
      showDownloadErrorTip(data){
        if(!this.isListening){
          return;
        }
        try{
          data = JSON.parse(data)
        }catch(e){
          return ;
        }

        if(data && data.msg && data.s == this.sign){
          this.msgError(data.msg);
          document.getElementById(this.iframeName).innerhtml="";
          document.getElementById(this.iframeName).src="";
        }
      },
      addErrorTipListener(){
        if (window.addEventListener) {
          window.addEventListener("message", (e)=>{
            this.showDownloadErrorTip(e.data);
          });
        } else {
          window.attachEvent("message", (e)=>{
            this.showDownloadErrorTip(e.data);
          });
        }
        this.isListening = true;
      },
      deleteErrorTipListener(){
        if(window.removeEventListener){
          window.removeEventListener("message");
        }else{
          window.detachEvent("message");
        }
      }
    }
  }
</script>

<style scoped>
  .download-iframe-page{
    margin-top: 5px;
    display: none;
  }
</style>
